var WXUtil = {

    root:"",

    setPrefix :function(root){
        this.root = root;
    },


    getUrlParam:function(name){
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        var r = window.location.search.substr(1).match(reg);  //匹配目标参数
        if (r!=null) return unescape(r[2]); return null; //返回参数值
    },


    hideOptionMenu:function(){
        document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
            WeixinJSBridge.call('hideOptionMenu');
        });
    },

    showOptionMenu:function(){
        document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
            WeixinJSBridge.call('showOptionMenu');
        });
    },

    getAppid:function(generalid,redirect){
        $.ajax({
            url:this.root+"/wxutil/getAppid",
            type:"POST",
            data:{generalid:generalid,ts:(new Date()).getTime()},
            dataType:"json",
            async:false,
            success:function(data){

                var appid  = data.appid;
                if(!appid){
                    return data;
                }
                redirect(appid);
            },
            error:function(jqXHR,textStatus){
                return {errcode:"01",errmsg:"获取公众号的appid失败!"}
            }
        })
    },


    toGetUserinfo:function(generalid){
        var redirect_uri = window.location.href;
        var index = redirect_uri.indexOf("#rd");
        if(index > -1) {
            redirect_uri = redirect_uri.substr(0,index);
        }
        this.getAppid(generalid,function(appid){
            //微信授权的固定url
            var wx_oauth2_url = "https://open.weixin.qq.com/connect/oauth2/authorize";
            var param = "?appid="+appid+"&redirect_uri="+encodeURIComponent(redirect_uri)+"&response_type=code&scope=snsapi_base&state=hyxt#wechat_redirect";
            var url = wx_oauth2_url+param;
            window.location.href = url;
            return true;
        });
    },



    toGetWxUser:function(generalid,redirect_uri){
        //var redirect_uri = window.location;
        this.getAppid(generalid,function(appid){
            //微信授权的固定url
            var wx_oauth2_url = "https://open.weixin.qq.com/connect/oauth2/authorize";
            var param = "?appid="+appid+"&redirect_uri="+encodeURIComponent(redirect_uri)+"&response_type=code&scope=snsapi_userinfo&state=hc#wechat_redirect";
            var url = wx_oauth2_url+param;

            setTimeout(function(){window.location.href = url;},50);
            return true;
        });
    },


    getWxUser:function(generalid,callback){
        var code = this.getUrlParam("code");
        $.ajax({
            url:this.root+"/wxutil/getWxUserInfo",
            type:"POST",
            data:{generalid:generalid,code:code,ts:(new Date()).getTime()},
            dataType:"json",
            success:function(data){
                callback(data);
            },
            error:function(){
                return {errcode:"01",errmsg:"获取公众号的appid失败!"}
            }
        });

    },

    getUser:function(generalid,callback){
        var code = this.getUrlParam("code");
        $.ajax({
            url:this.root+"/wxutil/getUserInfo",
            type:"POST",
            data:{generalid:generalid,code:code,ts:(new Date()).getTime()},
            dataType:"json",
            success:function(data){
                callback(data);
            },
            error:function(){
                return {errcode:"01",errmsg:"获取公众号的appid失败!"}
            }
        });
    },

    checkMParam:function(value){
        if(value != null && value != undefined && value != "" && value != "null" && value != "NULL" && value != "undefined" && value != "UNDEFINED"){
            return true;
        }
        return false;
    }

}


